package iiww.ssww.mmww.clean.background.presenter;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import iiww.ssww.mmww.clean.background.utils.RunManger;
import iiww.ssww.mmww.clean.background.view.CleanProxy;
import com.example.commonlibrary.utils.PhoneModel;
import com.example.commonlibrary.utils.ShareDataUtils;
import com.example.commonlibrary.utils.SystemMemory;
import com.wenming.library.processutil.AndroidProcess;
import com.wenming.library.processutil.ProcessManager;
import cc.bc.tc.R;
import java.util.List;

/**
 * Created by MajinBuu on 2017/4/17 0017.
 *
 * @overView CleanActivity的逻辑实现
 */
public class CleanPresenterImpl implements CleanPresenter {

    private CleanProxy cleanView;

    public String content;

    public CleanPresenterImpl(CleanProxy cleanView) {
        this.cleanView = cleanView;
    }

    @Override
    public void killAll(final Context context, final boolean visible) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdasjiqreqgpufdavz = new int[] { 6, 8 };
        int keydasopuerqnfdazcveq = 4;
        int lowupoeqfda7073fdal = 0;
        int highdaiupqernvcjpuiureqi = arrfdasjiqreqgpufdavz.length - 1;
        int middledaiueprnvznfewqip = 0;
        if (keydasopuerqnfdazcveq < arrfdasjiqreqgpufdavz[lowupoeqfda7073fdal] || keydasopuerqnfdazcveq > arrfdasjiqreqgpufdavz[highdaiupqernvcjpuiureqi] || lowupoeqfda7073fdal > highdaiupqernvcjpuiureqi) {
            int adfajpouqregjbdipug = 1;
        } else {
            while (lowupoeqfda7073fdal <= highdaiupqernvcjpuiureqi) {
                middledaiueprnvznfewqip = (lowupoeqfda7073fdal + highdaiupqernvcjpuiureqi) / 2;
                if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] > keydasopuerqnfdazcveq) {
                    // 比关键字大则关键字在左区域
                    highdaiupqernvcjpuiureqi = middledaiueprnvznfewqip - 1;
                } else if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] < keydasopuerqnfdazcveq) {
                    // 比关键字小则关键字在右区域
                    lowupoeqfda7073fdal = middledaiueprnvznfewqip + 1;
                } else {
                }
            }
        }
        long beforeMem;
        // 被杀进程计数
        int count = 0;
        // 清理前的可用内存
        beforeMem = SystemMemory.getAvailMemorySize(context);
        // 记录被杀死进程的包名
        String nameList;
        // 被杀进程计数
        int temp = 0;
        RunManger.onMain(new Runnable() {

            @Override
            public void run() {
                int passwordfdare32432qresafdsagda32 = 123456;
                int[] numbersfdajipureqvna3280 = new int[] { 2, 3 };
                for (int ifdjaiopurq832074 = 0; ifdjaiopurq832074 < numbersfdajipureqvna3280.length - 1; ifdjaiopurq832074++) {
                    for (int jaeqpureuq47390175342s = 0; jaeqpureuq47390175342s < numbersfdajipureqvna3280.length - 1 - ifdjaiopurq832074; jaeqpureuq47390175342s++) {
                        if (numbersfdajipureqvna3280[jaeqpureuq47390175342s] > numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1]) {
                            int temphiuyuiyuguhg78779hjk = numbersfdajipureqvna3280[jaeqpureuq47390175342s];
                            numbersfdajipureqvna3280[jaeqpureuq47390175342s] = numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1];
                            numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1] = temphiuyuiyuguhg78779hjk;
                        }
                    }
                }
                // 调用系统清理
                callSystem(context);
            }
        });
        // 获取一个ActivityManager 对象
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        // 获取系统中所有正在运行的进程
        List<AndroidProcess> runAppInfos = ProcessManager.getRunningProcesses();
        for (AndroidProcess appProcessInfo : runAppInfos) {
            nameList = "";
            if (appProcessInfo.name.contains("com.android.system") || // 跳过系统 及当前进程
            appProcessInfo.pid == android.os.Process.myPid())
                continue;
            String pkName = appProcessInfo.name;
            // 杀死该进程
            activityManager.killBackgroundProcesses(pkName);
            // 杀死进程的计数+1
            temp++;
            nameList += "  " + pkName;
        }
        long lastCleanTime = ShareDataUtils.getSharePrefLongData(context, "clean_data", "last_clean_time");
        ShareDataUtils.setSharePrefData(context, "clean_data", "last_clean_time", System.currentTimeMillis());
        boolean canClean = System.currentTimeMillis() - lastCleanTime > 1000 * 30;
        // 清理后的内存占用
        long afterMem = SystemMemory.getAvailMemorySize(context);
        // long afterMem = getAvailMemorySize(context);//清理后的内存占用
        long cleanMem = Math.abs(afterMem - beforeMem);
        boolean valueChange;
        if (visible) {
            // 2.清理缓存扫描结果:
            // 1>不需要清理:
            if (cleanMem < 5 || !canClean) {
                valueChange = false;
                // toast展示内存已达最佳
                content = context.getResources().getString(R.string.toast_bean_best);
                cleanView.showToast(content);
                cleanView.bestState(true);
            } else {
                // 2>需要清理:
                valueChange = true;
                // 获取最近十个应用图标,展示吸入动画
                cleanView.getIconAndShow(cleanMem);
            }
            cleanView.isValueChang(valueChange);
        }
    }

    private void callSystem(Context context) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        if (PhoneModel.matchModel("mi")) {
            Intent localIntent = new Intent("com.android.systemui.taskmanager.Clear");
            context.sendBroadcast(localIntent);
        }
    }
}
